﻿@using DTcms.Common;
@using System.Data;
@{
    ViewBag.Title = "编辑角色";
    List<SelectListItem> roleList = ViewBag.roleList;
    DataTable list = ViewBag.navList;
}
@model DTcms.Model.manager_role
<form id="form1" class="layui-form">
    @Html.AntiForgeryToken()
    <!--导航栏-->
    <div class="pageheader">
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li><a href="../manager/role_list" class="back"><i class="fa fa-mail-reply-all"></i> 返回列表页</a></li>
                <li><a href="javascript:;"><i class="iconfont icon-home1"></i> 首页</a> </li>
                <li><a href="../manager/manager_list">管理员</a> </li>
                <li><a href="javascript:;">管理角色</a> </li>
                <li><a href="javascript:;">编辑角色</a> </li>
            </ul>
        </div>
    </div>
    <!--/导航栏-->
    <!--内容-->
    <div class="layui-fluid">
        <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title" id="floatHead">
                <li class="layui-this">编辑角色信息</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色类型：</label>
                        <div class="layui-input-block">
                            @Html.DropDownList("ddlRoleType", new SelectList(roleList, "Value", "Text", Model.role_type), new { @lay_filter = "role", datatype = "*", errormsg = "请选择角色类型！", sucmsg = "" })
                            <span class="Validform_checktip">*</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色名称：</label>
                        <div class="layui-input-block">
                            @Html.TextBox("txtRoleName", Model.role_name, new { @class = "input normal", datatype = "*1-100", sucmsg = " " })
                            <span class="Validform_checktip">*角色中文名称，100字符内</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">管理权限</label>
                        <div class="layui-input-block">
                            <table border="0" cellspacing="0" cellpadding="0" class="border-table" width="98%" id="ltable">
                                <thead>
                                    <tr>
                                        <th width="30%">导航名称</th>
                                        <th>权限分配</th>
                                        <th width="10%">全选</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (DataRow dr in list.Rows)
                                    {
                                        string[] ss = dr["action_type"].ToString().Split(',');
                                        <tr>
                                            <td style="white-space:nowrap;word-break:break-all;overflow:hidden;">
                                                <label class="hide">@dr["name"]</label>
                                                <label class="hide">@dr["action_type"]</label>
                                                <label class="hide">@dr["class_layer"]</label>
                                                @if (Convert.ToInt32(dr["class_layer"]) == 1)
                                                {
                                                    <i class="icon iconfont icon-folder-empty"></i>
                                                }
                                                else
                                                {
                                                    <span style="display:inline-block;width:@((Utils.ObjToInt(dr["class_layer"])-2)*24)px;"></span><i class="icon iconfont icon-csac"></i><i class="icon iconfont icon-folder-empty"></i>
                                                }
                                                @dr["title"]
                                            </td>
                                            <td>
                                                <span class="cbllist">
                                                    @for (int i = 0; i < ss.Length; i++)
                                                    {
                                                        if (Utils.ActionType().ContainsKey(ss[i]))
                                                        {
                                                            DTcms.Model.manager_role_value model = null;
                                                            if (Model.manager_role_values != null)
                                                            {
                                                                model = Model.manager_role_values.Find(p => p.nav_name == dr["name"].ToString() && p.action_type == ss[i]);
                                                            }
                                                            if (model != null)
                                                            {
                                                                <input type="checkbox" id="cbllist_@dr["id"]_@i" value="@ss[i]" checked="checked" title="@Utils.ActionType()[ss[i]]">
                                                            }
                                                            else
                                                            {
                                                                <input type="checkbox" id="cbllist_@dr["id"]_@i" value="@ss[i]" title="@Utils.ActionType()[ss[i]]">
                                                            }
                                                        }
                                                    }
                                                </span>
                                            </td>
                                            <td align="center"><input type="checkbox" lay-filter="checkAll" title="全选"></td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--/内容-->
    <!--工具栏-->
    <div class="page-footer">
        <div class="btn-wrap">
            <button class="layui-btn icon-btn" id="btnSubmit" lay-submit lay-filter="btnSubmit"><i class="layui-icon layui-icon-ok"></i>提交保存</button>
            <button class="layui-btn layui-btn-warm icon-btn" name="btnReturn" onclick="javascript:history.back(-1);"><i class="layui-icon layui-icon-return"></i>返回上一页</button>
        </div>
    </div>
    <!--/工具栏-->
</form>
@section Scripts{
    <script type="text/javascript">
        layui.use(['form', 'validform', 'jqueryform','element', 'notice'], function () {
            var form = layui.form, $ = layui.jquery, notice = layui.notice;
            //初始化表单验证
            $("#form1").initValidform('#btnSubmit', 1);

            //是否启用权限
            if ($("#ddlRoleType").val() == 1) {
                $(".border-table").find("input[type='checkbox']").prop("disabled", true);
                form.render('checkbox');
            }
            form.on('select(role)', function(data){
                if (data.value == 1) {
                    $(".border-table").find("input[type='checkbox']").prop("checked", false);
                    $(".border-table").find("input[type='checkbox']").prop("disabled", true);
                } else {
                    $(".border-table").find("input[type='checkbox']").prop("disabled", false);
                }
                form.render('checkbox');
            });

            //权限全选
            form.on('checkbox(checkAll)', function (data) {
                var child = $(data.elem).parent().siblings("td").find("input[type='checkbox']");
                child.each(function (index, item) {
                    item.checked = data.elem.checked;
                });
                form.render('checkbox');
            });

            //监听提交
            form.on('submit(btnSubmit)', function (data) {
                var $row = $("#ltable tr:gt(0)");
                var json = { list: [] };
                $($row).each(function (i, item) {
                    var name = $(item).find("td:eq(0)").find("label:eq(0)").text();
                    var arr = new Array();
                    var chks = $(item).find(".cbllist input:checkbox");
                    var i = 0;
                    $(chks).each(function (_i, ctl) {
                        if ($(ctl).prop("checked")) {
                            arr[i] = $(ctl).val();
                            i++;
                        }
                    });
                    var obj = { name: name, action_type: arr };
                    json.list.push(obj);
                });
                if (data.field.txtRoleName == "" || data.field.ddlRoleType == "") {
                    layer.msg("信息提交错误！");
                    return false;
                }
                var sendUrl = "ManagerRoleSave?action=@ViewBag.action&id=@ViewBag.id";
                $.ajax({
                    url: sendUrl,
                    type: "POST",
                    dataType: "json",
                    data: { json: JSON.stringify(json), role_name: data.field.txtRoleName, role_type: data.field.ddlRoleType },
                    success: function (res) {
                        if (res.status == 1) {
                            notice.success({
                                message: res.msg,
                                timeout: 2000,
                                position: 'topCenter',
                                onClosed: function () {
                                    if (res.url == "back") {
                                        history.back(-1);
                                    } else if (res.url != "") {
                                        location.href = res.url;
                                    }
                                }
                            });
                        }
                        else {
                            notice.error({ message: res.msg, timeout: 2000, position: 'topCenter' });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        notice.error({ message: '状态：' + textStatus + '；出错提示：' + errorThrown, timeout: 2000, position: 'topCenter' });
                    }
                });
                return false;
            });
        });
    </script>
}